package com.mycat.common.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

/**
 * 简单的httpclient请求
 * 
 * @author Roger
 * @date 2016年6月13日
 */
@SuppressWarnings("deprecation")
public class HttpUtils {
	
	/**
	 * 执行一个HTTP POST请求，返回请求响应的HTML
	 * 
	 * @param url
	 *            请求的URL地址
	 * @param params
	 *            请求的查询参数,可以为null
	 * @return 返回请求响应的HTML
	 */
	public static String doPost(String url, Map<String, String> params) {
		String response = null;
			HttpClient client = new HttpClient();
			PostMethod postMethod = new PostMethod(url);
			postMethod.getParams().setParameter(
					HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
			// 设置Post数据
			if (!params.isEmpty()) {
				int i = 0;
				NameValuePair[] data = new NameValuePair[params.size()];
				for (Entry<String, String> entry : params.entrySet()) {
					data[i] = new NameValuePair(entry.getKey(), entry.getValue());
					i++;
				}
				postMethod.setRequestBody(data);

		}
		try {
			client.executeMethod(postMethod);
			if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
				response = postMethod.getResponseBodyAsString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			postMethod.releaseConnection();
		}
		return response;
	}

	/**
	 * 执行一个HTTP POST请求，返回请求响应的JSON
	 * 
	 * @param url
	 *            请求的URL地址
	 * @param json
	 *            请求的参数
	 * @return
	 */
	public static String doPost(String url, JSONObject json) {
		DefaultHttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);

		String result = null;
		try {
			StringEntity s = new StringEntity(json.toString(), "UTF-8");
//			s.setContentEncoding("UTF-8");
			s.setContentType("application/json");
			post.setEntity(s);
			HttpResponse res = client.execute(post);
			if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				result = EntityUtils.toString(res.getEntity());
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}finally{
			post.releaseConnection();
			client.close();
		}
		return result;
	}

	/**
	 * 执行一个HTTP GET请求，返回请求响应的HTML
	 * 
	 * @param url
	 *            请求的URL地址
	 * @return 返回请求响应的HTML
	 */
	public static String doGet(String url) {
		String response = null;
		HttpClient client = new HttpClient();
		GetMethod getMethod = new GetMethod(url);
		try {
			client.executeMethod(getMethod);
			if (getMethod.getStatusCode() == HttpStatus.SC_OK) {
				response = getMethod.getResponseBodyAsString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			getMethod.releaseConnection();
		}
		return response;
	}

	public static void main(String[] args) {
		String get = HttpUtils.doGet("http://192.168.0.134:8080/privilege/getAdminList/1/10?staffName=罗杰");
		System.out.println(get);


		/**
		 * list1(@RequestBody BodyDTO bodyDTO)
		 */
//		JSONObject jsonObject = new JSONObject();
//		jsonObject.put("id",1);
//		jsonObject.put("name","哈哈呵呵");
//		jsonObject.put("text","哈哈呵呵1111ssdfd");
//		String jsonStr = HttpUtils.doPost("http://localhost:8080/admin/list1",jsonObject);
//		System.out.println(jsonStr);


		/**
		 * list3(@RequestParam("id") String id,@RequestParam("name") String name,@RequestParam("text") String text)
		 */
//		Map<String, String> params = new HashMap<String, String>(){{
//			this.put("id","1");
//			this.put("name","哈哈呵呵");
//			this.put("text","哈哈呵呵111");
//		}};
//		String jsonStr1 = HttpUtils.doPost("http://localhost:8080/admin/list3",params);
//		System.out.println(jsonStr1);



	}

}
